package com.jkkc.lib.base.util

import android.app.Activity
import androidx.fragment.app.Fragment
import com.jkkc.lib.base.extend.match
import java.util.*
import kotlin.system.exitProcess

/**
 * Author:LiXian
 * <p>
 * Desc: Activity堆栈式管理
 * Version:1.0
 * date 2023/4/7 9:34
 */
object AppManager {

    private val activityStack: Stack<Activity> = Stack()
    private val fragmentStack: Stack<Fragment> = Stack()

    @JvmStatic
    fun getActivityStack(): Stack<Activity> = activityStack

    @JvmStatic
    fun getFragmentStack(): Stack<Fragment> = fragmentStack

    /**
     * 添加Activity到堆栈
     */
    @JvmStatic
    fun addActivity(activity: Activity?): Boolean {
        return activity?.let { activityStack.add(it) } ?: false
    }

    /**
     * 移除指定的Activity
     */
    @JvmStatic
    fun removeActivity(activity: Activity?): Boolean {
        return activity?.let { activityStack.remove(it) } ?: false
    }

    /**
     * 是否有Activity
     */
    @JvmStatic
    fun isHadActivity(): Boolean = activityStack.isNotEmpty()

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    @JvmStatic
    fun getCurrentActivity(): Activity? = if (isHadActivity()) activityStack.lastElement() else null


    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    @JvmStatic
    fun finishActivity() {
        val activity = activityStack.lastElement()
        finishActivity(activity)
    }

    /**
     * 结束指定的Activity
     */
    @JvmStatic
    fun finishActivity(activity: Activity) {
        if (!activity.isFinishing) {
            activity.finish()
            removeActivity(activity)
        }
    }

    /**
     * 结束指定类名的Activity
     */
    @JvmStatic
    fun finishActivity(cls: Class<*>) {
        for (activity in activityStack) {
            if (activity.javaClass == cls) {
                finishActivity(activity)
                break
            }
        }
    }

    /**
     * 结束所有Activity
     */
    @JvmStatic
    fun finishAllActivity() {
        activityStack.forEach { finishActivity(it) }
        activityStack.clear()
    }

    /**
     * 获取指定的Activity
     */
    @JvmStatic
    fun getActivityByCls(cls: Class<*>): Activity? {
        return activityStack.match { it.javaClass == cls }
    }

    /**
     * 添加Fragment到堆栈
     */
    @JvmStatic
    fun addFragment(fragment: Fragment?): Boolean {
        return fragment?.let { fragmentStack.add(it) } ?: false
    }

    /**
     * 移除指定的Fragment
     */
    @JvmStatic
    fun removeFragment(fragment: Fragment?): Boolean {
        return fragment?.let { fragmentStack.remove(it) } ?: false
    }

    /**
     * 是否有Fragment
     */
    @JvmStatic
    fun isFragment(): Boolean = fragmentStack.isNotEmpty()

    /**
     * 获取当前Fragment（堆栈中最后一个压入的）
     */
    @JvmStatic
    fun getCurrentFragment(): Fragment = fragmentStack.lastElement()

    /**
     * 退出应用程序
     */
    @JvmStatic
    fun exitApp() {
        activityStack.clear()
        fragmentStack.clear()
        try {
            // 杀死该应用进程
            android.os.Process.killProcess(android.os.Process.myPid());
            exitProcess(0);
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}